package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IEvaluateActivityModel;
import com.wgts.wgts.mvp.presenter.IEvaluateActivityPresenter;
import com.wgts.wgts.mvp.view.IEvaluateActivityView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.EvaluateClassRequest;
import com.wgts.wgts.netmodle.req.IssueEvaluateRequest;
import com.wgts.wgts.netmodle.resp.CurrentEntity;
import com.wgts.wgts.netmodle.resp.EvaluateEntity;

import java.util.ArrayList;
import java.util.List;

/*
 *
 *
 *   data:2018/10/1510:55
 *
 */public class EvaluateActivityPresenter implements IEvaluateActivityPresenter{
     private IEvaluateActivityView view;
     private IEvaluateActivityModel model;
     private List<EvaluateEntity> entityList;
     //评价参数
     private IssueEvaluateRequest request;
     private List<EvaluateClassRequest> evaluateList;

    public EvaluateActivityPresenter(IEvaluateActivityView view, IEvaluateActivityModel model) {
        this.view = view;
        this.model = model;
    }

    @Override
    public void initList(String orderId) {
        entityList = new ArrayList<>();
        model.initList(orderId, new HttpResultSubscriber<List<EvaluateEntity>>() {
            @Override
            public void onSuccess(List<EvaluateEntity> evaluateEntities) {
                entityList = evaluateEntities;
                for (int i = 0; i < entityList.size(); i++){
                    for (int j = 0; j < entityList.get(i).comments.size(); j++){
                        entityList.get(i).comments.get(j).isSelect = false;
                        entityList.get(i).comments.get(j).Pposition = i;
                    }
                }
                view.initList(entityList);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void checkEvaluate(Integer Pposition, Integer position) {
        for (int i = 0; i < entityList.get(Pposition).comments.size(); i++){
            entityList.get(Pposition).comments.get(i).isSelect = false;
        }
        entityList.get(Pposition).comments.get(position).isSelect = true;
        view.checkNotify();
    }

    @Override
    public void ratingChange(int position, int score) {
        entityList.get(position).score = score;
    }

    @Override
    public void isAllHaveEvaluate() {
        List<Boolean> allProductEvaluate = new ArrayList<>();
        boolean isAllEvaluate = true;

        for (int i = 0; i < entityList.size(); i++){
            boolean isEvaluate = false;
            for (int j = 0; j < entityList.get(i).comments.size(); j++){
                if (entityList.get(i).comments.get(j).isSelect){
                    isEvaluate = true;
                    continue;
                }
            }
            allProductEvaluate.add(i, isEvaluate);
        }

        for (int i = 0; i < allProductEvaluate.size(); i++){
            if (!allProductEvaluate.get(i)){
                isAllEvaluate = false;
                continue;
            }
        }

        view.haveEvaluate(isAllEvaluate);
    }

    @Override
    public void issueEvaluate(String orderId) {
        request = new IssueEvaluateRequest();
        request.orderId = orderId;
        evaluateList = new ArrayList<>();
        for (int i = 0; i < entityList.size(); i++){
            EvaluateClassRequest classRequest = new EvaluateClassRequest();
            for (int j = 0; j < entityList.get(i).comments.size(); j++){
                if (entityList.get(i).comments.get(j).isSelect){
                    classRequest.content = entityList.get(i).comments.get(j).msg;
                }
            }
            classRequest.productId = entityList.get(i).productId;
            classRequest.score = entityList.get(i).score;
            classRequest.productStandard = entityList.get(i).productStandard;
            classRequest.images = "";
            evaluateList.add(classRequest);
        }
        request.evaluateList = evaluateList;
        model.issueEvaluate(request, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                view.issueEvaluate();
            }

            @Override
            public void _onError(int status, String msg) {
                    view.onError(status, msg);
            }
        });
    }
}
